package com.tarena.lbs.basic.web.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.basic.web.mapper.StoreMapper;
import com.tarena.lbs.pojo.basic.entity.StoreSearchEntity;
import com.tarena.lbs.pojo.basic.po.StorePO;
import com.tarena.lbs.pojo.basic.query.StoreQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StoreRepository {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private StoreESRepository storeESRepository;

    public PageInfo<StorePO> getstores(StoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        //拼接查询的条件  只判断businessId
        if (query.getBusinessId() != null) {
            queryWrapper.eq("business_id", query.getBusinessId());
        }
        //开启PageHelper分页
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<StorePO> pos = storeMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }

    public void save(StorePO poParam) {
        //新增数据到mysql
        storeMapper.insert(poParam);
        //新增数据到ES
        //1.配置ES项目环境 使用repository接口 定义entity文档对象
        //2.手写底层API代码 组织storeEntity新增-RestHighLevelClient
        StoreSearchEntity entity = new StoreSearchEntity();
        BeanUtils.copyProperties(poParam, entity);
        entity.setLocation(poParam.getStoreLatitude() + "," + poParam.getStoreLongitude());//将经纬度拼接
        storeESRepository.save(entity);
    }
}
